package com.heima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.StringUtils;


import com.heima.common.result.PageResult;
import com.heima.mapper.VmTypeMapper;
import com.heima.pojo.dto.PageDTO;
import com.heima.pojo.entity.VmType;
import com.heima.service.VmTypeService;
import org.springframework.stereotype.Service;

/**
 * \* User: chy
 * \* Date: 2025/8/21  20:58
 * \* Description:
 * \
 */
@Service
public class VmTypeServiceImpl extends ServiceImpl<VmTypeMapper, VmType> implements VmTypeService {
    /**
     * 设备类型分页查询
     * @param vmTypePageDTO
     * @return
     */
    @Override
    public PageResult<VmType> pageVmType(PageDTO vmTypePageDTO) {
        //用mybatisPlus实现分页,实现分页条件查询
        Page<VmType> page = new Page<>(vmTypePageDTO.getPageIndex(), vmTypePageDTO.getPageSize());
        // 构建查询条件
        // 使用LambdaQueryWrapper
        LambdaQueryWrapper<VmType> queryWrapper = new LambdaQueryWrapper<VmType>()
                .like(StringUtils.hasText(vmTypePageDTO.getName()), VmType::getName, vmTypePageDTO.getName());

        Page<VmType> pageResult = this.page(page, queryWrapper);//这里this是VmTypeServiceImpl
        return PageResult.<VmType>builder()
                .pageIndex(vmTypePageDTO.getPageIndex())
                .pageSize(vmTypePageDTO.getPageSize())
                .totalPage(Math.toIntExact(pageResult.getPages()))
                .totalCount(Math.toIntExact(pageResult.getTotal()))
                .currentPageRecords(pageResult.getRecords())
                .build();

    }
}
